package com.wgq.uitest.exp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.wgq.uitest.MainActivity;
import com.wgq.uitest.R;

public class Line extends AppCompatActivity {

    //获得根布局下的第一个线性布局
    private LinearLayout root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line);

        root = findViewById(R.id.root);

        final LinearLayout.LayoutParams params9 = new LinearLayout.LayoutParams(root.getLayoutParams());

        final Button btn1 = (Button) findViewById(R.id.quit);
        final Button btn2 = (Button) findViewById(R.id.horizontal);
        final Button btn3 = (Button) findViewById(R.id.vertical);
        final Button btn4 = (Button) findViewById(R.id.left);

        //返回
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Line.this, MainActivity.class);
                startActivity(intent);
            }
        });

        //设置水平布局距离
        ViewGroup.LayoutParams params = btn2.getLayoutParams();
        final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(params.width, params.height);
        lp.setMargins(20, 400, 0, 0);

        ViewGroup.LayoutParams params2 = btn1.getLayoutParams();
        final LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(params2.width, params2.height);
        lp2.setMargins(0, 320, 0, 0);

        ViewGroup.LayoutParams params3 = btn3.getLayoutParams();
        final LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(params3.width, params3.height);
        lp3.setMargins(40, 400, 0, 0);

        ViewGroup.LayoutParams params4 = btn4.getLayoutParams();
        final LinearLayout.LayoutParams lp4 = new LinearLayout.LayoutParams(params4.width, params4.height);
        lp4.setMargins(40, 400, 0, 0);

        //设置水平布局，同时恢复中心对齐
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                params9.gravity = Gravity.CENTER;
                root.setLayoutParams(params9);
                root.setOrientation(LinearLayout.HORIZONTAL);
                btn2.setLayoutParams(lp);
                btn1.setLayoutParams(lp2);
                btn3.setLayoutParams(lp3);
                btn4.setLayoutParams(lp4);
            }
        });

        //设置垂直布局距离
        ViewGroup.LayoutParams params5 = btn2.getLayoutParams();
        final LinearLayout.LayoutParams lp5 = new LinearLayout.LayoutParams(params5.width, params5.height);
        lp5.setMargins(0, 340, 0, 0);

        ViewGroup.LayoutParams params6 = btn1.getLayoutParams();
        final LinearLayout.LayoutParams lp6 = new LinearLayout.LayoutParams(params6.width, params6.height);
        lp6.setMargins(0, 150, 0, 0);

        ViewGroup.LayoutParams params7 = btn3.getLayoutParams();
        final LinearLayout.LayoutParams lp7 = new LinearLayout.LayoutParams(params7.width, params7.height);
        lp7.setMargins(0, 0, 0, 0);

        ViewGroup.LayoutParams params8 = btn4.getLayoutParams();
        final LinearLayout.LayoutParams lp8 = new LinearLayout.LayoutParams(params8.width, params8.height);
        lp8.setMargins(0, 0, 0, 0);

        //设置垂直布局，同时恢复中心对齐
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                params9.gravity = Gravity.CENTER;
                root.setLayoutParams(params9);
                root.setOrientation(LinearLayout.VERTICAL);
                btn2.setLayoutParams(lp5);
                btn1.setLayoutParams(lp6);
                btn3.setLayoutParams(lp7);
                btn4.setLayoutParams(lp8);
            }
        });

        //设置左对齐
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                params9.gravity = Gravity.LEFT;
                root.setLayoutParams(params9);
            }
        });
    }
}
